/***************************************************************************
 *   Copyright (C) 2006 by Grzegorz Latarowski                             *
 *   g.latarowski@gmail.com                                                *
 *                                                                         *
 *   Project: AutoStop (kodowa nazwa do zmiany)                            *
 *                                                                         *
 ***************************************************************************/


#ifndef FILTERDIALOG_H
#define FILTERDIALOG_H


#include <QtGui>
#include <QtCore>
#include <QtSql>
#include "mainwindow.h"
#include "sortfilterproxymodels.h"


class TemplateFilterWidget;
class FilterWidget;

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class FilterWizardDialog : public QDialog
{
Q_OBJECT;

public:
        /*!
        * Konstruktor.
        */
    FilterWizardDialog(MainWindow *parent = 0, Qt::WFlags f = 0);

    QGridLayout *getGridLayout() { return gridLayout; }
    
    Filter *filter() { return _filter; }
    
public slots:        
    void createFilterWidgets(int);
    void acceptFilters();

private:
    MainWindow *_parent;
    int _currentRow;
    
    QList<QComboBox *> operatorComboBoxList;
    QList<FilterWidget *> filterWidgetList;
    
    Filter *_filter;
    
    QLabel *columnLabel;
    QLabel *operatorLabel;
    QLabel *value1Label;
    QLabel *value2Label;
    QToolButton *addToolButton;
    QToolButton *removeToolButton;
    QPushButton *okPushButton;
    QPushButton *cancelPushButton;
    QGridLayout *gridLayout;
    QFrame *horizontalLine;
    
}; // class FilterWizardDialog
// ---------------------------------------------------------------------------




// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class FilterWidget : public QWidget
{
Q_OBJECT;

public:
        /*!
        * Konstruktor.
        */
    FilterWidget(int row, FilterWizardDialog *parent = 0, Qt::WFlags f = 0);
    FilterModel *filterModel();
    bool isActive() { return _active; }
    
public slots: 
    void columnActivated(int column);
    void setActive(bool b) { _active = b; emit activated(b); }
    
signals:
    void activated(bool b);
    
private:
    int _row;
    FilterWizardDialog *_parent;
    QSqlTableModel *_header;
    QComboBox *columnComboBox;
    TemplateFilterWidget *templateFilterWidget;
    QGridLayout *gridLayout;
    
    bool _active;
};
// ---------------------------------------------------------------------------




// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class TemplateFilterWidget : public QWidget
{
Q_OBJECT;

public:
    TemplateFilterWidget(QWidget *parent = 0, Qt::WFlags f = 0)
    :QWidget(parent, f)
    {
        _active = false;
        connect(this, SIGNAL(activated(bool)), this, SLOT(setActive(bool)));
    }
    
    virtual FilterModel *filterModel() { return new FilterModel(); }
    
    bool isActive() { return _active; }
    
public slots:
    void setActive(bool b) { _active = b;}
    
signals:
    void activated(bool b);
    
private:
    bool _active;
    
}; // class TemplateFilterWidget
// ---------------------------------------------------------------------------






// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class StringFilterWidget : public TemplateFilterWidget
{
Q_OBJECT;

public:
        /*!
        * Konstruktor.
        */
    StringFilterWidget(QStringList *dataList,
                       int column,
                       QWidget *parent = 0,
                       Qt::WFlags f = 0);
        /*!
        * Zwraca utworzony model. NULL jeśli nie wybrano kryteriów.
        */
    FilterModel * filterModel();
        /*!
        * Ustawia listę stringów do comboBoxa.
        */
    void setDataList(const QStringList *dataList);
    
private slots:
    void checkActive(int index);
    
private:
        /// Lista stringów do comboBoxa.
    QStringList _dataList;
    int _column;
    
    QComboBox *dataComboBox;
    
}; // class StringFilterWidget
// ---------------------------------------------------------------------------






// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class LimitedStringFilterWidget : public TemplateFilterWidget
{
Q_OBJECT;

public:
        /*!
        * Konstruktor.
        */
    LimitedStringFilterWidget(int column,
                              QWidget *parent = 0,
                              Qt::WFlags f = 0);
        /*!
        * Zwraca utworzony model. NULL jeśli nie wybrano kryteriów.
        */
    FilterModel * filterModel();

private slots:
    void checkActive();

private:
    int _column;

    QLineEdit *lineEdit;
    
}; // class LimitedStringFilterWidget
// ---------------------------------------------------------------------------






// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class UIntFilterWidget : public TemplateFilterWidget
{
Q_OBJECT;

public:
        /*!
        * Konstruktor.
        */
    UIntFilterWidget(int column, QWidget *parent = 0, Qt::WFlags f = 0);
        /*!
        * Zwraca utworzony model. NULL jeśli nie wybrano kryteriów.
        */
    FilterModel * filterModel();

private slots:
    void checkActive();
            
private:
    int _column;
    
    QLabel *fromLabel;
    QLabel *toLabel;
    QLineEdit *lowerBoundEdit;
    QLineEdit *upperBoundEdit;
    
}; // class UIntFilterWidget
// ---------------------------------------------------------------------------




// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class DoubleFilterWidget : public TemplateFilterWidget
{
Q_OBJECT;

public:
        /*!
        * Konstruktor.
        */
    DoubleFilterWidget(int column, QWidget *parent = 0, Qt::WFlags f = 0);
        /*!
        * Zwraca utworzony model. NULL jeśli nie wybrano kryteriów.
        */
    FilterModel * filterModel();

        
private slots:
    void checkActive();
    
private:
    int _column;
    
    QLabel *fromLabel;
    QLabel *toLabel;
    QLineEdit *lowerBoundEdit;
    QLineEdit *upperBoundEdit;
    
}; // class UIntFilterWidget
// ---------------------------------------------------------------------------



// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
class DateFilterWidget : public TemplateFilterWidget
{
Q_OBJECT;

public:
        /*!
        * Konstruktor.
        */
    DateFilterWidget(int column, QWidget *parent = 0, Qt::WFlags f = 0);
        /*!
        * Zwraca utworzony model. NULL jeśli nie wybrano kryteriów.
        */
    FilterModel * filterModel();

private slots:
    void checkActive(const QDate & date);
                
private:
    int _column;
    
    QLabel *fromLabel;
    QLabel *toLabel;
    QDateEdit *lowerBoundEdit;
    QDateEdit *upperBoundEdit;

}; // class UIntFilterWidget
// ---------------------------------------------------------------------------




#endif

